package cn.wolfcode.luowowo.packagetour.service.impl;

import cn.wolfcode.luowowo.packagetour.query.PackageTourSearchQueryObject;
import cn.wolfcode.luowowo.packagetour.repository.IPackageTourTemplateRepository;
import cn.wolfcode.luowowo.packagetour.service.IPackageTourService;
import cn.wolfcode.luowowo.packagetour.template.PackageTourTemplate;
import cn.wolfcode.luowowo.packagetour.vo.PackageTourResultVo;
import com.alibaba.dubbo.config.annotation.Service;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.sort.SortBuilders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;

import java.util.Iterator;
import java.util.List;

@Service
public class PackageTourServiceImpl implements IPackageTourService {
    @Autowired
    private IPackageTourTemplateRepository dao;
    @Autowired
    private ElasticsearchTemplate template;
    @Autowired
    private TransportClient client;

    //保存数据
    public void save(PackageTourTemplate packageTour) {
        dao.save(packageTour);
    }

    //搜索指定id的跟团游信息
    public PackageTourTemplate searchById(Long id) {
        Iterable<PackageTourTemplate> iterable = dao.search(QueryBuilders.matchQuery("id", id));
        Iterator<PackageTourTemplate> iterator = iterable.iterator();
        if (iterator.hasNext()){
            return iterator.next();
        }
        return new PackageTourTemplate();
    }

    //创建索引
    public void createIndex() {
        template.createIndex(PackageTourTemplate.class);
        template.getMapping(PackageTourTemplate.class);
    }

    //查询符合条件的跟团游信息
    public Page<PackageTourTemplate> searchByCondition(PackageTourSearchQueryObject qo) {

        BoolQueryBuilder boolQuery = QueryBuilders.boolQuery();
        PageRequest pageble = PageRequest.of(qo.getCurrentPage() - 1, qo.getPageSize());
        //旅行时间
        if (qo.getStartTime() != null){
            boolQuery.filter(QueryBuilders.rangeQuery("startTime").gte(qo.getStartTime()[0]));
        }
        //价格
        if(qo.getPrice() != null && qo.getPrice()[0].length() > 4){
            String minPrice = qo.getPrice()[0].split(":")[0];
            String maxPrice = qo.getPrice()[qo.getPrice().length - 1].split(":")[1];
            boolQuery.filter(QueryBuilders.rangeQuery("minPrice").gte(minPrice).lt(maxPrice));
        }

        //是否大促
        if (qo.getIsBigPromotion() != null) {
            boolQuery.filter(QueryBuilders.matchQuery("isBigPromotion", qo.getIsBigPromotion()));
        }
        //产品特色
        if(qo.getFeature() !=null && qo.getFeature().length > 0){
            for (Integer num : qo.getFeature()) {
                boolQuery.should(QueryBuilders.matchQuery("feature", num));
            }
        }
        //价格排序
        if(qo.getPriceOrder() != null && qo.getPriceOrder().length() > 0){
            if (qo.getPriceOrder().length() == 4){
                pageble = PageRequest.of(qo.getCurrentPage() - 1, qo.getPageSize(), new Sort(Sort.Direction.DESC, "minPrice"));
            }else{
                pageble = PageRequest.of(qo.getCurrentPage() - 1, qo.getPageSize(), new Sort(Sort.Direction.ASC, "minPrice"));
            }
        }
        //旅游方式
        if(qo.getTravelWay() != 0){
            boolQuery.must(QueryBuilders.matchQuery("travelWay", qo.getTravelWay()));
        }
        //出发地
        boolQuery.must(QueryBuilders.matchQuery("startPlace_id", qo.getStartPlace()));
        //目的地
        if (qo.getTravelDestName() != null && qo.getTravelDestName().length() > 0){
            boolQuery.must(QueryBuilders.matchQuery("destName", qo.getTravelDestName()));
        }

        Page<PackageTourTemplate> page = dao.search(boolQuery, pageble);
        return page;




    }
}
